package org.itboys.mobile.controller.mobile;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.SundayAdvertisementDto;
import org.itboys.mobile.dto.product.SundayProductCategoryDto;
import org.itboys.mobile.dto.product.SundayProductDto;
import org.itboys.mobile.service.mongo.mobile.SundayMobileAdvertisementService;
import org.itboys.mobile.service.mysql.mobile.product.SundayMobileProductCategoryService;
import org.itboys.mobile.service.mysql.mobile.product.SundayMobileProductService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：天街_金融_全球购_首页_controller_mobile
 */
@Controller
@RequestMapping("/tianjie/mobile/finance/globe/index")
public class SundayMobileIndexController extends BaseController {
    @Autowired
    private SundayMobileAdvertisementService advertisementService;
    @Autowired
    private SundayMobileProductCategoryService categoryService;
    @Autowired
    private SundayMobileProductService productService;


    /**
     *
     * @param request
     * @param response
     */
    @RequestMapping(value="/getIndexData",method= {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value="【全球购-首页】获取首页数据")
    public void getCart(HttpServletRequest request,
             HttpServletResponse response,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            Map<String,Object> result = new HashMap<String,Object>();

            //轮播图
            List<SundayAdvertisementDto> rolls=advertisementService.getAdvertisementByZone("index_roll");
            result.put("rolls",rolls);
            //新闻
            List<SundayAdvertisementDto> news=advertisementService.getAdvertisementByZone("index_news");
            result.put("news",news);
            //分类
            List<SundayProductCategoryDto> categorys=categoryService.getParentCategory();
            result.put("categorys",categorys);
            //首页推荐商品
            List<SundayProductDto> products=productService.getRecommendProduct();
            result.put("products",products);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/mobile/finance/globe/cart/getCart----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }


}
